package lib

import (
	"crypto/md5"
	"encoding/hex"
	"encoding/json"
	"fmt"
	"math/rand"
	"strconv"
	"time"

	_ "github.com/go-sql-driver/mysql"
)

func ResearchDel(s []string, p string) []string {
	for in := 0; in < len(s); in++ {
		if s[in] == p {
			s = append(s[:in], s[in+1:]...)
			in--
		}
	}
	return s
}

func Paging(page interface{}, total int) (start int, end int) {
	var i int
	switch v := page.(type) {
	case string:
		i, _ = strconv.Atoi(v)
	case int:
		i = v
	}
	start = i*total - total
	end = total
	return
}

func Axis() []string {
	var s []string
	currentTime := time.Now()
	for a := 0; a < 7; a++ {
		oldTime := currentTime.AddDate(0, 0, -a)
		s = append(s, oldTime.Format("2006-01-02"))
	}
	return s
}

func GenWorkid() string {
	rand.Seed(time.Now().UnixNano())
	a := rand.Intn(1000)
	c := strconv.Itoa(a)
	now := time.Now()
	return now.Format("20060102150405") + c
}

func Intersect(o, n []string) []string {
	m := make(map[string]int)
	var arr []string
	for _, v := range o {
		m[v]++
	}
	for _, v := range n {
		m[v]++
		if m[v] > 1 {
			arr = append(arr, v)
		}
	}
	return arr
}

func NonIntersect(o, n []string) []string {
	m := make(map[string]int)
	var arr []string
	for _, v := range o {
		m[v]++
	}
	for _, v := range n {
		m[v]++
		if m[v] == 1 {
			arr = append(arr, v)
		}
	}
	return arr
}

func Time2StrDiff(delay string) time.Duration {
	if delay != "none" {
		now := time.Now()
		dt, _ := time.ParseInLocation("2006-01-02 15:04 ", delay, time.Local)
		after := dt.Sub(now)
		if after+1 > 0 {
			return after
		}
	}
	return 0
}

//func TimeDifference(t string) bool {
//	dt, _ := time.ParseInLocation("2006-01-02 15:04 ", t, time.Local)
//	f := dt.Sub(time.Now())
//	if math.Abs(f.Minutes()) > float64(model.GloOther.ExQueryTime) && float64(model.GloOther.ExQueryTime) > 0 {
//		return true
//	}
//	return false
//}

type querydata struct {
	Field []map[string]string
	Data  []map[string]interface{}
}

func removeDuplicateElement(addrs []string) []string {
	result := make([]string, 0, len(addrs))
	temp := map[string]struct{}{}
	idx := 0
	for _, item := range addrs {
		if _, ok := temp[item]; !ok {
			temp[item] = struct{}{}
			result = append(result, item)
		} else {
			idx++
			item += fmt.Sprintf("(%v)", idx)
			result = append(result, item)
		}
	}
	return result
}

func JsonStringify(i interface{}) []byte {
	o, _ := json.Marshal(i)
	return o
}

func removeDuplicateElementForRule(addrs []string) []string {
	result := make([]string, 0, len(addrs))
	temp := map[string]struct{}{}
	for _, item := range addrs {
		if _, ok := temp[item]; !ok {
			temp[item] = struct{}{}
			result = append(result, item)
		}
	}
	return result
}

//func MulitUserRuleMarge(group []string) model.PermissionList {
//	var u model.PermissionList
//	for _, i := range group {
//		var k model.CoreRoleGroup
//		model.DB().Where("name =?", i).First(&k)
//		var m1 model.PermissionList
//		_ = json.Unmarshal(k.Permissions, &m1)
//		u.DDLSource = append(u.DDLSource, m1.DDLSource...)
//		u.DMLSource = append(u.DMLSource, m1.DMLSource...)
//		u.QuerySource = append(u.QuerySource, m1.QuerySource...)
//		u.Auditor = append(u.Auditor, m1.Auditor...)
//	}
//	u.DDLSource = removeDuplicateElementForRule(u.DDLSource)
//	u.DMLSource = removeDuplicateElementForRule(u.DMLSource)
//	u.Auditor = removeDuplicateElementForRule(u.Auditor)
//	u.QuerySource = removeDuplicateElementForRule(u.QuerySource)
//
//	return u
//}

func Md5(s string) string {
	// 将密码转换为字节数组
	sBytes := []byte(s)

	// 计算 MD5 哈希值
	hash := md5.Sum(sBytes)

	// 将哈希值转换为十六进制字符串
	hashString := hex.EncodeToString(hash[:])
	return hashString
}
